package com.mtw.bbs.like.controller.provide;


import cn.hutool.core.collection.CollectionUtil;
import com.mtw.bbs.common.core.vo.Result;
import com.mtw.bbs.article.pojo.form.LikeCountForm;
import com.mtw.bbs.article.pojo.vo.LikeCountVo;
import com.mtw.bbs.like.service.LikeCountService;
import com.mtw.bbs.like.service.LikeProviderService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("provide/like")
public class LikeProvider {


    private final LikeCountService likeCountService;
    private final LikeProviderService likeProviderService;



    /**
     * 根据username获取用户信息
     */
    @ApiOperation(value = "获取用户获赞总数", notes = "传入用户id")
    @GetMapping("/getUserLikeCount")
    public Result<Integer> getUserLikeCount(@RequestParam("uid") String uid) {
        return Result.success(likeCountService.getUserLikeCount(uid));
    }


    /**
     * 根据用户id，业务id和实体id判断用户是否已点赞
     */
    @PostMapping("/getLiked")
    public Result<LikeCountVo> getLiked(@RequestBody LikeCountForm form) {

        if (StringUtils.isBlank(form.getObjectType()) || StringUtils.isBlank(form.getObjectId()) || StringUtils.isBlank(form.getUserMid())) {
            return Result.fail("参数为空");
        }
        return Result.success(likeProviderService.getLiked(form.getObjectType(),form.getObjectId(),form.getUserMid()));
    }


    /**
     * 根据用户id，业务id和实体id集合判断用户是否已点赞
     */
    @PostMapping("/getLikedList")
    public Result<List<LikeCountVo>> getLikedList(@RequestBody LikeCountForm form) {

        if (StringUtils.isBlank(form.getObjectType()) || CollectionUtil.isEmpty(form.getObjectIds()) || StringUtils.isBlank(form.getUserMid())) {
            return Result.fail("参数为空");
        }
        return Result.success(likeProviderService.getLikedList(form.getObjectType(),form.getObjectIds(),form.getUserMid()));
    }

    /**
     * 根据用户id，业务id和实体id判断用户是否已点赞，并带出点赞量
     */
    @PostMapping("/getLikedWithCount")
    public Result<LikeCountVo> getLikedWithCount(@RequestBody LikeCountForm form) {

        if (StringUtils.isBlank(form.getObjectType()) || StringUtils.isBlank(form.getObjectId())) {
            return Result.fail("参数为空");
        }
        return Result.success(likeProviderService.getLikedWithCount(form.getObjectType(),form.getObjectId(),form.getUserMid()));
    }


    /**
     * 根据用户id，业务id和实体id集合判断用户是否已点赞，并带出点赞量
     */
    @PostMapping("/getLikedListWithCount")
    public Result<List<LikeCountVo>> getLikedListWithCount(@RequestBody LikeCountForm form) {

        if (StringUtils.isBlank(form.getObjectType()) || CollectionUtil.isEmpty(form.getObjectIds())) {
            return Result.fail("参数为空");
        }
        return Result.success(likeProviderService.getLikedListWithCount(form.getObjectType(),form.getObjectIds(),form.getUserMid()));
    }


    /**
     * 根据用业务id和实体id获取点赞数量
     */
    @PostMapping("/getCount")
    public Result<LikeCountVo> getCount(@RequestBody LikeCountForm form) {

        if (StringUtils.isBlank(form.getObjectType()) || StringUtils.isBlank(form.getObjectId())) {
            return Result.fail("参数为空");
        }
        return Result.success(likeProviderService.getCount(form.getObjectType(),form.getObjectId()));
    }


    /**
     * 根据用业务id和实体id集合获取点赞数量
     */
    @PostMapping("/getCountList")
    public Result<List<LikeCountVo>> getCountList(@RequestBody LikeCountForm form) {

        if (StringUtils.isBlank(form.getObjectType()) || CollectionUtil.isEmpty(form.getObjectIds())) {
            return Result.fail("参数为空");
        }
        return Result.success(likeProviderService.getCountList(form.getObjectType(),form.getObjectIds()));
    }


    /**
     * 根据用业务id和用户id获取已点对象id
     * @param userId        用户id
     * @param page          当前页
     * @param size          分页大小
     * @param objectType    业务对象
     */
    @GetMapping("/getUserLikeList/{userId}")
    public Result<List<String>> getUserLikeList(@PathVariable("userId") String userId,
                                                     @RequestParam(name = "page") int page,
                                                     @RequestParam(name = "size") int size,
                                                     @RequestParam(name = "objectType") String objectType) {

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(objectType)) {
            return Result.fail("参数为空");
        }
        return Result.success(likeProviderService.getUserLikeList(userId,page,size,objectType));
    }


}
